package com.hyzx.qbasic.admin.controller.cms;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.common.security.ShiroUtils;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.po.CmsContentPO;
import com.hyzx.qbasic.admin.model.po.CmsPagedContentPO;
import com.hyzx.qbasic.admin.model.po.CommonAttachmentPO;
import com.hyzx.qbasic.admin.model.vo.req.PagingRequest;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.common.util.UuidUtils;
import com.hyzx.qbasic.admin.domain.service.ICmsContentService;
import com.hyzx.qbasic.admin.domain.service.ICommonFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 黄江华 on 2018/7/20
 */


@Api(tags = "内容发布", description = "ContentController")
@Controller
@RequestMapping("/cms/content")
public class ContentController extends BaseController {

    @Autowired
    private ICmsContentService contentService;

    @Autowired
    private ICommonFileService commonFileService;

    @ApiIgnore
    @SysLog("内容发布首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.CMS_CONTENT_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.CMS_CONTENT_EDIT));
        model.addAttribute("publishFunc", getFunction(PermissionConst.CMS_CONTENT_PUBLISH));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.CMS_CONTENT_DELETE));
        model.addAttribute("restoreFunc", getFunction(PermissionConst.CMS_CONTENT_RESTORE));

        return "cms/content/list";
    }

    @ApiOperation("获取内容列表")
    @GetMapping("/getContentList")
    @ResponseBody
    public BaseResModel<PageInfo<CmsPagedContentPO>> getContentList(@Valid GetCmsContentListReq req, BindingResult result) {
        return BaseResUtils.success(contentService.getPagedCmsContentList(req));
    }

    @ApiIgnore
    @SysLog("新增内容页")
    @GetMapping("/add")
    public String add(Model model) {
        model.addAttribute("contentId", UuidUtils.getObjectId());
        return "cms/content/add";
    }

    @ApiOperation("新增内容表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddCmsContentReq req, BindingResult result) {
        req.setUserId(ShiroUtils.getCurrentUserId());
        req.setUserName(ShiroUtils.getCurrentUserName());

        BaseOperationRes res = contentService.addCmsContent(req);
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("内容预览页")
    @GetMapping("/preview/{contentId}")
    public String preview(@PathVariable("contentId") String contentId, Model model) {
        CmsContentPO cmsContent = contentService.getCmsContentInfo(contentId);
        cmsContent = cmsContent != null ? cmsContent : new CmsContentPO();
        model.addAttribute("cmsContent", cmsContent);

        List<CommonAttachmentPO> attachmentList = commonFileService.getAttachmentList(contentId);
        if (CollectionUtils.isNotEmpty(attachmentList))
            model.addAttribute("attachmentList", attachmentList);

        return "cms/content/preview";
    }

    @ApiIgnore
    @SysLog("编辑内容页")
    @GetMapping("/edit")
    public String edit() {
        return "cms/content/edit";
    }

    @ApiOperation("编辑内容表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody AddCmsContentReq req, BindingResult result) {
        req.setUserId(ShiroUtils.getCurrentUserId());
        req.setUserName(ShiroUtils.getCurrentUserName());

        BaseOperationRes res = contentService.editCmsContent(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取内容信息")
    @GetMapping("/getContentInfo")
    @ResponseBody
    public BaseResModel<CmsContentPO> getContentInfo(@Valid GetCmsContentInfoReq req, BindingResult result) {
        CmsContentPO cmsContent = contentService.getCmsContentInfo(req.getContentId());
        return BaseResUtils.success(cmsContent);
    }

    @ApiOperation("删除内容")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody UpdateCmsContentStatusReq req, BindingResult result) {
        BaseOperationRes res = contentService.deleteContent(req.getContentIdList());
        return BaseResUtils.success(res);
    }

    @ApiOperation("回收站还原内容")
    @PostMapping("/restore")
    @ResponseBody
    public BaseResModel<BaseOperationRes> restore(@Valid @RequestBody UpdateCmsContentStatusReq req, BindingResult result) {
        BaseOperationRes res = contentService.restoreContent(req.getContentIdList());
        return BaseResUtils.success(res);
    }

    @ApiOperation("内容发布")
    @PostMapping("/publish")
    @ResponseBody
    public BaseResModel<BaseOperationRes> publish(@Valid @RequestBody UpdateCmsContentStatusReq req, BindingResult result) {
        int publisher = ShiroUtils.getCurrentUserId();
        BaseOperationRes res = contentService.publish(publisher, req.getContentIdList());
        return BaseResUtils.success(res);
    }

    @ApiOperation("内容审核")
    @PostMapping("/audit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> audit(@Valid @RequestBody ProcessResultReq req, BindingResult result) {
        int auditor = ShiroUtils.getCurrentUserId();
        BaseOperationRes res = contentService.audit(auditor, req.getBusinessKey());
        return BaseResUtils.success(res);
    }

    @ApiOperation("内容撤回")
    @PostMapping("/withdraw")
    @ResponseBody
    public BaseResModel<BaseOperationRes> withdraw(@Valid @RequestBody ProcessResultReq req, BindingResult result) {
        int auditor = ShiroUtils.getCurrentUserId();
        BaseOperationRes res = contentService.withdraw(auditor, req.getBusinessKey(), req.getComment());
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("内容审核首页")
    @GetMapping("/auditList")
    public String auditList(Model model) {
        model.addAttribute("auditFunc", getFunction(PermissionConst.CMS_CONTENT_AUDIT));
        model.addAttribute("withdrawFunc", getFunction(PermissionConst.CMS_CONTENT_WITHDRAW));

        return "cms/content/auditList";
    }

    @ApiOperation("获取内容审核列表")
    @GetMapping("/getContentAuditList")
    @ResponseBody
    public BaseResModel<PageInfo<CmsPagedContentPO>> getContentAuditList(@Valid PagingRequest req, BindingResult result) {
        int currentUserId = ShiroUtils.getCurrentUserId();
        return BaseResUtils.success(
                contentService.getPagedAuditListInfo(req.getPageNum(), req.getPageSize(), currentUserId));
    }
}
